package br.com.senacrs.servlet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import br.com.senacrs.control.UsuarioCR;
import br.com.senacrs.model.UsuarioEN;
import br.com.senacrs.util.User;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mauricio
 */
@WebServlet(name = "LogonServlet", urlPatterns = {"/LogonServlet"})
public class LogonServlet extends HttpServlet {
    public static final String USERNAME = "nome";
    public static final String PASSWORD = "senha";
    public static final String NEXT = "next";
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter(USERNAME);
        String password = request.getParameter(PASSWORD);
        String next = request.getParameter(NEXT);

        if (next == null || next.length() == 0) {
            next = "Usuario";
        }
        //seta parametros do usuario e chama login
        UsuarioEN usuarioEN = new UsuarioEN();
        usuarioEN.setNome(username);
        usuarioEN.setSenha(password);
        usuarioEN = new UsuarioCR().logar(usuarioEN);
        if (usuarioEN != null) {
            request.getSession().setAttribute(UsuarioEN.KEY, usuarioEN);
            response.sendRedirect(next);
        } else {
            response.sendRedirect("login.jsp?fail=1");
        }
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
